//
//  Config.m
//  Weike
//
//  Created by leirz on 14-8-6.
//  Copyright (c) 2014年 winupon. All rights reserved.
//

#import "Config.h"
#import "AppUtils.h"


NSString *const ConfigGuideFlagPrefix = @"ConfigGuideFlag";
NSString *const  ConfigBindPushUserId = @"bind_push_user_id";
NSString *const ConfigGuideViewFollowTime = @"ConfigGuideViewFollowTime";



@implementation Config

+ (void)setValue:(NSString *)key value:(id)value
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setValue:value forKey:key];
    [userDefaults synchronize];
}

+ (id)getValue:(NSString *)key
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:key];
}

+ (BOOL)getBoolValue:(NSString *)key
{
    NSNumber *value = [self getValue:key];
    if (value == nil) {
        return NO;
    }
    return value.boolValue;
}

+ (void)saveMyHabitUpdateVersion:(NSString *)version ownerUserId:(NSString *)userId configType:(NSString*)type
{
    if([AppUtils isEmpty:version ])
    {
        version=@"0";
    }
    if(userId ==nil){
        userId=@"";
    }
    NSString *key = [NSString stringWithFormat:@"%@-%@",type, userId];
    [Config setValue:key value:version];
    
}

+ (NSString *)getMyHabitUpdateVersion:(NSString *)userId configType:(NSString*)type
{
    if(userId ==nil){
        userId=@"";
    }
    NSString *key = [NSString stringWithFormat:@"%@-%@",type, userId];
    NSString *version = [Config getValue:key];
    if([AppUtils isEmpty:version ])
    {
        version=@"0";
    }
    return version;
}

+ (void)saveArrayWithType:(NSString*) type userId:(NSString*)userId data:(NSMutableArray *)dataList
{
    if([AppUtils isEmpty:type ])
    {
        return;
    }
    if(dataList==nil)
    {
        dataList = [NSMutableArray array];
    }
    NSString *key = nil;
    if([AppUtils isEmpty:userId ]){
        key = type;
    }else{
        key = [NSString stringWithFormat:@"%@-%@",type, userId];
    }
    [Config setValue:key value:dataList];
}

+ (NSMutableArray *)getArrayWithType:(NSString*)type userId:(NSString*)userId
{
    if([AppUtils isEmpty:type ])
    {
        return [NSMutableArray array];
    }
    NSString *key = nil;
    if([AppUtils isEmpty:userId ]){
        key = type;
    }else{
        key = [NSString stringWithFormat:@"%@-%@",type, userId];
    }
     NSArray *arr =  (NSArray *) [Config getValue:key];
    if(arr==nil){
        return [NSMutableArray array];
    }
    return  [NSMutableArray arrayWithArray:arr];
}

+ (void)saveDictionaryWithType:(NSString*) type userId:(NSString*)userId data:(NSMutableDictionary *)dataDict
{
    if([AppUtils isEmpty:type ])
    {
        return;
    }
    if(dataDict==nil)
    {
        dataDict = [NSMutableDictionary dictionary];
    }
    NSString *key = nil;
    if([AppUtils isEmpty:userId ]){
        key = type;
    }else{
        key = [NSString stringWithFormat:@"%@-%@",type, userId];
    }
    [Config setValue:key value:dataDict];

}

+ (NSMutableDictionary *)getDictionaryWithType:(NSString*) type userId:(NSString*)userId
{
    if([AppUtils isEmpty:type ])
    {
        return [NSMutableDictionary dictionary];
    }
    NSString *key = nil;
    if([AppUtils isEmpty:userId ]){
        key = type;
    }else{
        key = [NSString stringWithFormat:@"%@-%@",type, userId];
    }
    NSDictionary *dict =  (NSDictionary *) [Config getValue:key];
    if(dict==nil){
        return [NSMutableDictionary dictionary];
    }
    return  [NSMutableDictionary dictionaryWithDictionary:dict];
}


@end
